<template>
  <div class="Login">
    <div class="login-box">
      <h1>管理员登录</h1>
      <el-form :inline="true" :model="user" class="demo-form-inline">
        <el-form-item label="账号">
          <el-input v-model="user.account" placeholder="邮箱号/用户名" clearable />
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="user.password" placeholder="密码" clearable />
        </el-form-item>
        <el-form-item label="验证码">
          <el-input v-model="user.captcha" placeholder="验证码" clearable />

          <canvas @click="foot" ref="canvas"></canvas>


        </el-form-item>
        <!-- 按钮 -->
        <el-form-item>
          <el-button class="btn" type="primary" @click="singinFn">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style lang="less">
@import url('../assets/less/public.less');

.Login {
  position: fixed;
  width: 100%;
  height: 100%;
  background-image: url('../assets/imgs/bg1.jpg');
  background-size: cover;
  background-repeat: no-repeat;
}

.login-box {
  display: flex;
  flex-direction: column;
  margin: 220px auto;
  align-items: center;
  width: 400px;
  background-color: rgba(255, 255, 255, 0.6);
  border-radius: 10px;

  h1 {
    margin: 10px auto;
    font-size: 20px;
    color: @textColor;
    border-bottom: 2px solid @bg;
  }

  .el-form-item__content {
    justify-content: space-between;
    align-items: center;

    canvas {
      margin-top: 15px;

    }
  }

  .demo-form-inline {
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    margin-left: 70px;

    .el-form-item {
      width: 80%;
      margin: 0;

      &:nth-child(3) {
        .el-input {
          width: 60%;
          margin-left: 5px
        }
      }

      .el-input {
        margin-top: 15px;
        margin-left: 18px;
        width: 100%;

      }
    }

    label {
      margin-top: 15px;
    }
  }

  .btn {
    margin-top: 15px;
    margin-bottom: 15px;
    width: 100%;
    margin-left: 0px;
  }
}
</style>
<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { signin, test } from '../../api/index'
import { ElMessage } from 'element-plus'
import api from '../../api/request'
import { useRouter } from 'vue-router';
let canvas = ref();
let pen: any
let w: number = 80
let h: number = 30
let host: string = api.url
console.log(host);

type U = {
  account: string
  password: string
  captcha: string
}
let user = ref<U>({
  account: '',
  password: '',
  captcha: ''
})
const $router = useRouter();

const singinFn = async () => {
  let { account, password, captcha } = user.value;
  console.log(user.value);
  
  // console.log({account , password ,captcha})
  if (account && password && captcha) {
    // console.log(1)
    // 先验证码是否为机器人
    let isShow;
    await test({ captcha })
      .then(
        content => {
          // console.log(content);
          if (content.code == 200) {
            // 执行登录逻辑
            isShow = true;
          }
          else {
            // 验证失败
            ElMessage({
              type: 'error',
              message: content.msg
            })
          }
        }
      )

    if (isShow) {
      // console.log({account , password, role: 1})
      // 执行登录逻辑
      await signin({ account, password, role: 1 })
        .then(
          content => {
            let { code } = content;
             console.log(content);
            if (code == 200) {
              let { nickName, token } = content;
              // 本地存储缓存用户昵称、临时身份
              localStorage.setItem('NICK_NAME', nickName)
              localStorage.setItem('TOKEN', token)
              // 登录成功
              ElMessage({
                type: 'success',
                message: content.msg
              })

              // 延迟跳转首页
              setTimeout(() => {
                $router.replace({ path: '/flowpath' })
              }, 150);
            }
            else {
              // 登陆失败
              ElMessage({
                type: 'error',
                message: content.msg
              })
            }
          }
        )
    }

  }
  else {
    // console.log(2)
    ElMessage({
      type: 'error',
      message: "输入框不能为空",
    })
  }
}

onMounted(() => {
  let ele = canvas.value;

  if (ele) {
    ele.width = w;
    ele.height = h;

    let img = new Image();
    img.src = `${host}/api/verify/captcha`
    pen = ele.getContext('2d');
    img.onload = () => {
      pen?.drawImage(
        img,
        0,
        0,
        w,
        h
      )
    }
    img.onerror = () => {
      console.error("加载验证码图像失败。");
      ElMessage({
        type: 'error',
        message: "无法加载验证码图像。",
      });
    };
  }
})
const foot = () => {
  let img = new Image();
  img.src = `${host}/api/verify/captcha?${Math.random()}`
  pen.clearRect(0,0,w,h)
  img.onload = () => {
    pen?.drawImage(
      img,
      0,
      0,
      w,
      h
    )
  }
}
</script>
